package com.wanxi.dangdang.controller.back.user;

import com.github.pagehelper.PageInfo;
import com.wanxi.dangdang.common.CommonResult;
import com.wanxi.dangdang.common.ResultCode;
import com.wanxi.dangdang.model.dto.*;
import com.wanxi.dangdang.model.pojo.Admin;
import com.wanxi.dangdang.model.vo.AdminFindByIdVo;
import com.wanxi.dangdang.model.vo.AdminFindPageVo;
import com.wanxi.dangdang.model.vo.AdminRoleVo;
import com.wanxi.dangdang.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: 吕永杰
 * @Description:
 * @Date: Created in 2021/8/30
 * @Version: 1.0
 */
@Api(tags={"管理员接口层（需要登录）"})
@RestController
@RequestMapping("/back/a/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @GetMapping("/checkAccount")
    @ApiOperation(value="验证账号是否存在", notes="验证账号是否存在，2004 用户不存在，2005用户存在")
    public CommonResult checkAccount(@ApiParam(value = "输入的账号", required = true) @RequestParam String account){
        String s = adminService.checkAccount(account);
        if(s!=null)
            return CommonResult.failure(ResultCode.USER_HAS_EXISTED);
        return CommonResult.failure(ResultCode.USER_NOT_EXIST);
    }

    @PostMapping("/add")
    @ApiOperation(value="新增管理员", notes="超级管理员添加管理员,管理员页面添加弹窗")
    public CommonResult add(@ApiParam(value = "新增管理员参数", required = true) @RequestBody AdminAddDto adminAddDto, HttpSession session){
        Admin admin = new Admin();
        admin.setSuperManagerId((Long) session.getAttribute("id"));
        BeanUtils.copyProperties(adminAddDto,admin);
        adminService.add(admin);
        return CommonResult.success();
    }

    @GetMapping("/findById")
    @ApiOperation(value="查询单个管理员", notes="查询单个管理员")
    public CommonResult<Admin> findById(@ApiParam(value = "管理员id", required = true) @RequestParam Long id){
        AdminFindByIdVo admin = adminService.findById(id);
        return CommonResult.success(admin);
    }

    @PostMapping("/findPage")
    @ApiOperation(value="超级管理员分页查询所有管理员")
    public CommonResult<List<PageInfo<AdminFindPageVo>>> findPage(@ApiParam(value = "模糊查询,page加limit", required = true) @RequestBody AdminFindPageDto adminFindPageDto){
        PageInfo pageInfo = adminService.findPage(adminFindPageDto);
        return CommonResult.success(pageInfo);
    }

    @PostMapping("/updateById")
    @ApiOperation(value="超级管理修改管理员")
    public CommonResult updateById(@ApiParam(value = "修改后的信息", required = true) @RequestBody AdminUpdateDto adminUpdateDto,HttpSession session){
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminUpdateDto,admin);
        admin.setSuperManagerId((Long) session.getAttribute("id"));
        adminService.updateById(admin);
        return CommonResult.success();
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "超级管理根据id删除管理员")
    public CommonResult deleteById(@ApiParam(value = "管理员id", required = true) @RequestParam Long id){
        adminService.deleteById(id);
        return CommonResult.success();
    }

    @PostMapping("/delAll")
    public CommonResult delAll(@RequestBody AdminDelAllDto adminDelAllDto) {
        adminService.delAll(adminDelAllDto);
        return CommonResult.success();
    }

    @PostMapping("/updateRolesById")
    @ApiOperation(value = "超级管理修改管理员的角色")
    public CommonResult updateRolesById(@ApiParam(value = "管理员id和现用管理员", required = true) @RequestBody AdminRoleVo adminRoleVo){
        adminService.updateRolesById(adminRoleVo);
        return CommonResult.success();
    }

    @PostMapping("/loginByPass")
    @ApiOperation(value = "登录后台")
    public CommonResult loginByPass(@ApiParam(value = "需要账号密码", required = true) @RequestBody AdminLoginDto adminLoginDto){
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminLoginDto,admin);
        String token = adminService.loginByPass(admin);
        if (token != null)
            return CommonResult.success(tokenHead + " " + token);
        return CommonResult.failure(ResultCode.USER_LOGIN_ERROR);
    }

    @GetMapping("/rePassword")
    @ApiOperation(value = "重置密码")
    public CommonResult rePassword(@ApiParam(value = "需要重置密码的id",required = true)@RequestParam Long id){
        adminService.rePassword(id);
        return CommonResult.success();
    }



}
